var currentwindow;
var counts = "";

mui.plusReady(function() {
	$("#all").html("");
	$("#all").html(
		'<a style="font-size:100px; color:rgba(242,125,72,1.00); display:block; text-align:center;" class="mui-icon mui-icon-search"></a><a class="mui-title" style="color:rgba(128,128,128,1.00); position: initial; margin: 0 auto; height:' +
		(window.screen.height - 200) + 'px">No data available at the moment</a>');
	if (window.plus && plus.nativeUI) {
		plus.nativeUI.closeWaiting();
	}

})


var UserName = localStorage.getItem("UserName");
//alert(UserNO);
function showinfo(e) {
	e.preventDefault();
	change2(1);
}

function openPosition(TradeOrderNO, ProductNO, SourceOrderNO) {

	// 显示选择对话框
	showSelectionDialog(TradeOrderNO, ProductNO, SourceOrderNO);

}

function showSelectionDialog(TradeOrderNO, ProductNO, SourceOrderNO) {
	// 创建对话框元素
	const dialog = document.createElement('div');
	dialog.className = 'selection-dialog';
	dialog.innerHTML = `
        <p>备料单号:${TradeOrderNO},请选择操作:</p>
        <button class="mui-btn mui-btn-primary" onclick="handleSelection('MP','${TradeOrderNO}','${ProductNO}','${SourceOrderNO}')">备料</button>
	    <button class="mui-btn mui-btn-royal" onclick="handleSelection('Ingredient','${TradeOrderNO}','${ProductNO}','${SourceOrderNO}')">配料</button>
        <button class="mui-btn mui-btn-danger" onclick="handleSelection('cancel', '${TradeOrderNO}', '${ProductNO}','${SourceOrderNO}')">取消操作</button>
    `;

	// 将对话框添加到 body
	document.body.appendChild(dialog);

	// 添加关闭对话框的事件监听器
	dialog.addEventListener('click', function(event) {
		if (event.target === dialog) {
			dialog.remove();
		}
	});
}

function handleSelection(action, TradeOrderNO, ProductNO, SourceOrderNO) {
	// 根据选择的操作执行相应的逻辑
	switch (action) {
		case 'cancel':
			break;
		case 'MP':
			mui.openWindow({
				url: 'MFG_MP_Edit.html',
				action: UserName,
				extras: {
					TradeOrderNO: TradeOrderNO,
					ProductNO: ProductNO,
					SourceOrderNO: SourceOrderNO
				}
			});
			break;
		case 'Ingredient':
			mui.openWindow({
				url: 'MFG_MP_BEdit.html',
				action: UserName,
				extras: {
					TradeOrderNO: TradeOrderNO,
					ProductNO: ProductNO,
					SourceOrderNO: SourceOrderNO
				}
			});
			break;
	}

	// 移除对话框
	document.querySelector('.selection-dialog').remove();
}


// function openPosition(TradeOrderNO, ProductNO, SourceOrderNO) {
// 	console.log(UserNO)
// 	mui.openWindow({
// 		url: 'WMSWorkOrder_GetMatial.html',
// 		action: UserNO,
// 		extras: {
// 			TradeOrderNO: TradeOrderNO,
// 			SourceOrderNO: SourceOrderNO,
// 			ProductNO: ProductNO
// 		}
// 	});
// }




function change2(e) {
	pageno = e;
	var pageall;
	//$("#all").html("");//全部清空
	var html = "";
	var TradeOrderNO = $("#TradeNO").val();
	var CreateTime = $("#Creatime").val();
	xccFormPostAsync("api/PDA/GetTradeInfo", {
		PageIndex: e,
		PageSize: 10,
		TradeOrderNO: TradeOrderNO,
		CreateTime: CreateTime,
		TradeType: 13,
		RState: 1
	}, function(data) {
		pageall = Math.ceil(parseInt(data.Items.length) / 10) //向上取整    parseInt:转换为INT类型
		if (data.Items.length != null && data.Items.length > 0) {
			$("#all").html(""); ////全部清空
			for (var i = 0; i < data.Items.length; i++) {
				$("#all").append(`<li class="mui-table-view-cell" onclick="openPosition(
					'${data.Items[i].TradeOrderNO}','${data.Items[i].ProductNO}','${data.Items[i].SourceOrderNO}')">
					<div class="list-title">OrderNO ${data.Items[i].TradeOrderNO}</div>
					<div class="list-detail">ProductNO: ${data.Items[i].ProductNO}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TradeQty:${data.Items[i].TradeQty}</div>
					<div class="list-detail">Warehouse: ${data.Items[i].WarehouseName}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Work:${data.Items[i].SourceOrderNO}</div>
					<div class="list-detail">Date: ${data.Items[i].CreateTime}</div></li>`)

				if (e < 1) {
					e = 1;
					pageno = 1; //就等于第1页 ， 当前页为1
				}
				if (pageall === 0) pageall = 1; // 确保最小为 1
				if (e > pageall) { //如果输入页大于最大页
					e = pageall;
					pageno = pageall; //输入页和当前页都=最大页
				}
				var pageContent = "";
				pageContent += 'page<span id=\"a2\">' + pageno + '</span>/';
				pageContent += '<span id="a1">' + pageall + '</span>';
				/*pageContent +='<span id="a3" >'+ye+'</span>';*/
				if (e > 1) { //页数大于1不能上一页
					pageContent += '<a href="#" onClick="change2(--pageno)" >previous page</a>';
				}
				if (e < pageall) { //页数小于不能上一页
					pageContent += '<a href="#" onClick="change2(++pageno)">next page</a>';
				}
				if (pageno <= pageall) {
					$("#page").html(pageContent);
				}

			}
			plus.nativeUI.closeWaiting();
		} else {
			$("#all").html("");
			$("#all").html(
				'<a style="font-size:100px; color:rgba(242,125,72,1.00); display:block; text-align:center;" class="mui-icon mui-icon-search"></a><a class="mui-title" style="color:rgba(128,128,128,1.00); position: initial; margin: 0 auto; height:' +
				(window.screen.height - 400) + 'px">No data available at the moment</a>');
			plus.nativeUI.closeWaiting();
		}
	}, function(errr) {

		console.error("查询出错:", errr);
		plus.nativeUI.closeWaiting();
	})
}